package edu.ycu.aladdin.collector.utils;

import edu.ycu.aladdin.collector.api.entity.CrawlerConfig;
import us.codecraft.webmagic.Site;

/**
 * 类型转换工具类.
 *
 * @author <a href="mailto:wangruihuano@gmail.com">王瑞环</a>
 * @since 1.0
 */
public class CoverUtil {

    public static CrawlerConfig parseFromSite(Site site) {

        CrawlerConfig crawlerConfig = new CrawlerConfig();
        crawlerConfig.setCharset(site.getCharset());
        crawlerConfig.setCycleRetryTimes(site.getCycleRetryTimes());
        crawlerConfig.setDomain(site.getDomain());
        crawlerConfig.setRetryTimes(site.getRetryTimes());
        crawlerConfig.setSleepTime(site.getSleepTime());
        crawlerConfig.setRetrySleepTime(site.getRetrySleepTime());
        crawlerConfig.setTimeOut(site.getTimeOut());
        crawlerConfig.setUserAgent(site.getUserAgent());
        return crawlerConfig;
    }

    public static Site parseFromConfig(CrawlerConfig crawlerConfig) {

        Site site = Site.me().setCharset(crawlerConfig.getCharset()).setCycleRetryTimes(crawlerConfig.getCycleRetryTimes())
                .setDomain(crawlerConfig.getDomain()).setRetryTimes(crawlerConfig.getRetryTimes())
                .setSleepTime(crawlerConfig.getSleepTime()).setRetrySleepTime(crawlerConfig.getRetrySleepTime())
                .setTimeOut(crawlerConfig.getTimeOut()).setUserAgent(crawlerConfig.getUserAgent());
        return site;
    }

}
